/***************************************************************************
 *   Copyright (C) 2008 by BogDan Vatra                                    *
 *   bogdan@licentia.eu                                                    *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/

#ifndef OPERATIUNICONTABILE_H
#define OPERATIUNICONTABILE_H

#include <QWebView>
#include <query.h>

#include "operatiunicontabilewidget.h"
#include <lookuplineedit.h>
#include <lineedit.h>
#include <analiticeInterface.h>

#include "ui_operatiunicontabile.h"

using namespace SQL;
using namespace KontAMabil;

class OperatiuniContabile : public KontAMabilWindow, private Ui::operatiuniContabileForm
{
	Q_OBJECT
private:
	struct _analitic
	{
		qint64 id;
		QString tipAnalitic;
		bool operator == (const _analitic & b){ return id==b.id && tipAnalitic==b.tipAnalitic;}
	};

	struct _analitice
	{
		QList<_analitic> debit;
		QList<_analitic> credit;
	};


public:
	OperatiuniContabile(QWidget* parent = 0, Qt::WFlags fl = 0);
	~OperatiuniContabile();

private:
	void hideNumar();
	void showOperatiuni();
	bool cautaAct(const QString & numarAct, const QString & serieAct);
	void adaugaOperatiune();
	void verificaAnaliticePentruCont(const QString & cont, AnaliticeInterface::TipCont tipCont);
	void populeazaAnalitice();
	void puneAnalitice();
	AnaliticeInterface * amPluginPentru(const QString & pluginName);

private slots:
	void on_cereSeriaButton_toggled(bool pushed);
	void on_notaEdit_focusIn();
	void on_ziuaEdit_focusIn();
	void on_ziuaEdit_returnPressed();
	void on_numarEdit_returnPressed();
	void on_numarEdit_escapePressed();
	void on_seriaEdit_returnPressed();
	void on_seriaEdit_escapePressed();
	void onAboutToClose();
	void onAboutToShow();
	void conturiEditingFinished();
	void sumaEditingFinished();

	void currentChanged(int col, int row);
	void editCell(int col, int row, const QRect & rect, const QString & text);
	void paintCell(int col, int row, QPainter & painter, const QRect & rect, int cellFlags);
	void deletePressed(int col, int row);

private:
	Query * m_acteQry;
	Query * m_opQry;
	Query * m_noteQry;
	Query * m_analiticeQry;
	OperatiuniContabileWidget * m_ocw;
	int m_curCol, m_curRan;
	LookupLineEdit * m_conturiEdit;
	LineEdit * m_sumaEdit;
	QString m_contc;
	QString m_contd;
	double m_suma;
	QList<AnaliticeInterface *> m_analiticePlugins;
	QMap<QString, QList<_analitic> > m_analiticeUnice;
	QVector< _analitice > m_analitice;
	QWebView m_infoView;
};

#endif

